//
//  MyManager.m
//  NZTaxi
//
//  Created by rix on 12/24/10.
//  Copyright 2010 List-it. All rights reserved.
//

#import "MyManager.h"
#import "SynthesizeSingleton.h"
#import "StringEncryption.h"
#import "NSData+Base64.h"

@implementation MyManager

SYNTHESIZE_SINGLETON_FOR_CLASS(MyManager)

@synthesize 
listItDomain, webServicePath, requestJobAPI, 
deviceUDID, customerPickupAddress, customerDestinationAddress,
customerName, customerMobileNo, isCustomerRegistered, registerCustomerDeviceAPI,
customerPickupAddressObj, customerDestinationAddressObj, specialInstructions;

// A singleton class to manage global variables 
-(id)init {	
	if (self = [super init]) {		
        
		self.listItDomain = @"http://www.list-it.co.nz";		
		self.webServicePath = @"/dev/SmartService.svc";
		self.requestJobAPI = @"/requestJob";
		self.registerCustomerDeviceAPI = @"/RegisterCustomerDevice";
        
		UIDevice *myDeviceUDID = [UIDevice currentDevice];
		self.deviceUDID = [myDeviceUDID uniqueIdentifier];
		
		NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
		if ([self isCustomerRegistered] == NO) {			
			if ([[preferences stringForKey:@"accnt-customer-name"] length] == 0) {
				self.isCustomerRegistered = NO;
			} else {
				self.isCustomerRegistered = YES;
				self.customerName = [preferences stringForKey:@"accnt-customer-name"];
				self.customerMobileNo = [preferences stringForKey:@"accnt-mobileno"];
			}
		} else {
			self.customerName = [preferences stringForKey:@"accnt-customer-name"];
			self.customerMobileNo = [preferences stringForKey:@"accnt-mobileno"];
		}
	}
	
	return self;
}

-(void)dealloc {	
	[listItDomain release];
	[webServicePath release];
	[requestJobAPI release];
	[deviceUDID release];
    [specialInstructions release];
    
	[super dealloc];
}

- (NSString *) secretKey {
    return [[NSString alloc] initWithString: @"mini426kaniko"];
}

- (NSString *) encrypt: (NSString *)string {
    StringEncryption *crypto = [[[StringEncryption alloc] init] autorelease];
    
	NSData *_secretData = [string dataUsingEncoding:NSUTF8StringEncoding];
	CCOptions padding = kCCOptionPKCS7Padding;
	NSData *encryptedData = [crypto encrypt:_secretData key:[[self secretKey] dataUsingEncoding:NSUTF8StringEncoding] padding:&padding];
    
    NSString *str = [[NSString alloc] initWithFormat:@"%@", encryptedData];
    
    return str;
}

- (NSString *) crypDeviceUDID {
    return [self encrypt: self.deviceUDID];
}

- (NSString *) crypCustomerPickupAddress {
    return [self encrypt: self.customerPickupAddress];
}

- (NSString *) crypCustomerDestinationAddress {
    return [self encrypt: self.customerDestinationAddress];
}

- (NSString *) crypCustomerName {
    NSLog(@"Customer name: %@", self.customerName);
    return [self encrypt: customerName];
}

- (NSString *) crypCustomerMobileNo {
    return [self encrypt: self.customerMobileNo];
}


@end
